package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserRealReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserRealRewardDaoImpl extends TempDataObjectDao<UserRealReward> {

    public static String getUserRealRewardSql = "select * from `t_u_real_reward` where `userId`=? order by `no` asc ";

    public List<UserRealReward> getUserRealRewardList(long userId) {
        List<UserRealReward> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserRealRewardSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateUserRealRewardSql = "update `t_u_real_reward` set goodsId=?,getTime=?,addrTime=?,addr=?,phone=? where userId=? and no=?";

    @Override
    protected boolean updateData(UserRealReward userRealReward) {
        userRealReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userRealReward.getGoodsId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userRealReward.getGetTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userRealReward.getAddrTime().getTime())));
        para.put(i++, new DbParameter(Types.VARCHAR, userRealReward.getAddr()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRealReward.getPhone()));
        para.put(i++, new DbParameter(Types.BIGINT, userRealReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userRealReward.getNo()));
        if (getDb().executeNoneQuery(updateUserRealRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserRealRewardSql = "insert into `t_u_real_reward`(userId,no,goodsId,getTime,addrTime,addr,phone) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserRealReward userRealReward) {
        userRealReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRealReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userRealReward.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, userRealReward.getGoodsId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userRealReward.getGetTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userRealReward.getAddrTime().getTime())));
        para.put(i++, new DbParameter(Types.VARCHAR, userRealReward.getAddr()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRealReward.getPhone()));
        if (getDb().executeNoneQuery(insertUserRealRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserRealReward parseData(ResultSet rs) throws SQLException {
        UserRealReward userRealReward = new UserRealReward();
        userRealReward.setUserId(rs.getLong("userId"));
        userRealReward.setNo(rs.getInt("no"));
        userRealReward.setGoodsId(rs.getInt("goodsId"));
        userRealReward.setGetTime(rs.getTimestamp("getTime"));
        userRealReward.setAddrTime(rs.getTimestamp("addrTime"));
        userRealReward.setAddr(rs.getString("addr"));
        userRealReward.setPhone(rs.getString("phone"));
        return userRealReward;
    }

}
